<?php
/**
 * Ajax类
 * @copyright   2011 友拓软件友拓通
 * @category   	通用
 * @package  	Action 
 * @version 	2011-03-25
 * @author 		邹燕娟
 */
class AjaxPublicAction extends CommonAction {

	public $_view_dir 			= 'Ajax';			/// tpl目录名	
	
	public function _initialize(){
		parent::_initialize();
		C('SHOW_PAGE_TRACE',false);
	}
	
	/// 构造函数
	public function __destruct(){
		exit;
	}

	/// 获取城市的autocomplete
	public function getCity() {
		$id = intval($_GET['id']);
		echo '<input type="hidden" name="city_id" id="city_id"><input type="text" name="city_name" url="'.U('AutoComplete/city').'" where="'.urlencode("parent_id='".$id."'").'" jqac/>';
	}

	/// 根据当前级别取产品上级类别信息
	public function getParentProductClass(){
		if (C('PRODUCT_CLASS_LEVEL')<=1) {
			return '';exit;
		}
		$id = intval($_GET['id']);
		if ($id<=0) { echo 'error';}
		$rs = M('ProductClass')->field('class_name,parent_id')->select($id);
		$html = $rs['class_name'];
		for ($i=0;;$i++) {
			if ($rs['parent_id']<=0) {break;}
			$rs = M('ProductClass')->field('class_name,parent_id')->select($rs['parent_id']);
			$html = $rs['class_name'].'->'.$html;
		}
		echo $html;
	}

	/// 判断颜色是否可删除
	public function checkProdcutColor(){
		$product_id = intval($_GET['product']);
		$color_id 	= intval($_GET['color']);
		$count 		= M('Storage')->where('product_id='.$product_id.' and color_id='.$color_id.' and quantity>0')->count();
		echo $count;
	}

	/// 判断尺码是否可删除
	public function checkProdcutSize(){
		$product_id = intval($_GET['product']);
		$size_id = intval($_GET['size']);
		$count = M('Storage')->where('product_id='.$product_id.' and size_id='.$size_id.' and quantity>0')->count();
		echo $count;
	}

	///退货获取产品ID
	public function getReturnSaleProductNoInput() {
		$client_id 		= intval($_POST['client_id']);
		$basic_id 		= intval($_POST['basic_id']);
	  ///$currency_id 	= intval($_POST['currency_id']);
		if($client_id==0 || $basic_id==0) {
			return false;
		}
		///销售单信息
		$sale_select	=	'select id from sale_order where client_id='.$client_id.' and basic_id='.$basic_id.' ';
		if(C('sale.relation_sale_follow_up')==1) {
			///发货
			$model	=	M('deliveryDetail');
			$info	= $model->field('group_concat(distinct(product_id)) as product_id')
			->where(' sale_order_id in ( '.$sale_select.' )')
			->select();
		}else {
			///销售
			$model	=	M('saleOrderDetail');
			$info	= $model->field('group_concat(distinct(product_id)) as product_id')
			->where(' sale_order_id in ( '.$sale_select.' )')
			->select();
		}
		if(!empty($info[0]['product_id'])) {		$extend	=	' sale_order_detail.product_id in ('.$info[0]['product_id'].')';	}else{	$extend	=	' sale_order_detail.product_id=0 ';}
		echo $extend;
		exit();
	}

	/// 根据银行ID获取对应的币种ID
	public function getBankCurrencyId(){
		$bank_id		=	$_GET['bank_id'];
		if ($bank_id>0){
			$bank_currency	=	S('bank_currency');
			$currency		=	$bank_currency[$bank_id];
		}
///		$currency['currency_id']	=	$currency_id;
		echo json_encode($currency);
		exit();
	}

	/// 获取company表的信息(厂家/客户等)
	public function getCompanyInfo() {
		$id = intval($_GET['id']);
		if ($id > 0) {
			$info = M("Company")->field('currency_id')->select($id);
			echo json_encode($info);
		}
		exit();
	}

	/// 获取产品信息(流程使用)
	public function getProductInfo() {
		$id 			= intval($_GET['id']);
		$color_id		= intval($_GET['color_id']); /// 颜色
		$size_id		= intval($_GET['size_id']); /// 尺码
		$currency_id	= intval($_GET['currency_id']); /// 根据币别取单价
		$flow			= trim($_GET['flow']);
		$type			= trim($_GET['type']);
		$mantissa		= trim($_GET['mantissa']);
		$adjust_date	= $_GET['adjust_date'];
		if ($id > 0 ) {
			$product = M("Product")->field('product_name,instock_price,sale_price,wholesale_price,retail_price,capability,dozen,weight,(cube_high*cube_wide*cube_long) as per_size')->select($id);
			$info 	 = $product[0];
			$info['autocomplete'] = false;
			if ($flow=='sale' || $type == 'sale_return') {
				$info['storage_color']								= 0;
				$info['storage_size']								= 0;
				$where												= ' and 1';
				if(C('storage_color')==1) $info['storage_color']	= 1;
				if(C('storage_size')==1)  $info['storage_size']		= 1;
				if($color_id>0) $where	.= ' and color_id='.$color_id;
				if($size_id>0) $where	.= ' and size_id='.$size_id;
				$temp = M('SaleStorage')->where('product_id='.$id.$where.' and quantity>0 and mantissa='.$mantissa)->select();
				if (count($temp)==1) {
					$info['capability'] 	= $temp[0]['capability'];
					$info['dozen'] 			= $temp[0]['dozen'];
				}else if(empty($temp)){
					$info['capability']		= '';
					$info['dozen']			= '';
					$info['autocomplete'] 	= false;
				}else {
					$info['capability'] 	= 0;
					$info['dozen'] 			= 0;
					$info['autocomplete'] 	= true;
				}
			}else {
				if (C('storage_format')==3) {
					$info['capability'] = $info['capability']/$info['dozen'];
				}
			}
			if($flow=='adjust'){
				if(C('currency')<>1){
					$date	= date('Y-m-d',strtotime(formatDate($adjust_date)));
					$rate 	= M('rate_info')
								->where("rate_date='".$date."' and from_currency_id=1 and to_currency_id=".C('currency'))
								->find();	
					if(!$rate['opened_y']){
						$fixed_rate	= M('fixed_rate')->where('from_currency_id=1 and to_currency_id='.C('currency'))->find();
						$info['instock_price']	= $info['instock_price']*$fixed_rate['rate'];
					}else{
						$info['instock_price']	= $info['instock_price']*$rate['opened_y'];
					}
				}
			}else{
				if ($currency_id <> 1) { /// 只有币种是人民币时才取预计进货单价
					$info['instock_price'] 	= 0;
				} 
			}
			/// 多客户类型
			if (C('MULTI_CLIENT') && $_GET['client_id'] > 0) {
				$client_type = M("Company")->where('comp_type=1 and id='.intval($_GET['client_id']))->find();
				$client_type = $client_type['detail_type'];
				$info['sale_price'] = $client_type == 1 ? $info['wholesale_price'] :
				( $client_type == 2 ? $info['retail_price'] : $info['sale_price']);
			}
			$field 		= 'last_price_'.$currency_id;
			$field_avg 	= 'avg_price_'.$currency_id;
			if (C('price_default') == 2) { /// 最近一次入库/销售价
				$last_price 			= M("ProductPrice")->where('product_id='.$id)->select();
				$info['instock_price'] 	= $last_price[$field] == null ? 0 : $last_price[$field];
				$info['sale_price'] 	= $last_price['sale_'.$field] == null ? 0 : $last_price['sale_'.$field];
			} else if (C('price_default') == 3) { /// 平均入库/销售单价
				$avg_price 				= M("ProductPrice")->where('product_id='.$id)->select();
				$info['instock_price'] 	= $avg_price[$field_avg] == null ? 0 : $avg_price[$field_avg];
				$info['sale_price'] 	= $avg_price['sale_'.$field_avg] == null ? 0 : $avg_price['sale_'.$field_avg];
			}
			/// 销售时币种与本位币不一致时取
			if ($currency_id <> C('CURRENCY')) {
				$info['sale_price']		= 0;
			}
			$info['show_delivery']		= 0;
			if(C('instock.delivery')==2){
				$info['show_delivery']		= 1;
			}
			$info['capability']==0 && $info['capability'] 		= '';
			$info['dozen']==0 && $info['dozen'] = '';
			$info = _formatArray($info);
			echo json_encode($info);
			exit();
		}
		exit();
	}


	/// 获取订单详情
	public  function getOrderDetails() {
		$detail_id 	= intval($_GET['detail_id']);
		$order_id	= intval($_GET['order_id']);
		$p_id		= intval($_GET['p_id']);
		$color_id	= intval($_GET['color_id']);
		$size_id	= intval($_GET['size_id']);
		/// 实例化类
		$model = D("Orders");
		///构造查询条件
		$where		= ' b.detail_state<3';
		$detail_id && $where.=" and b.id=".$detail_id;
		$order_id  && $where.=" and a.id=".$order_id;
		$p_id      && $where.=" and b.product_id=".$p_id ;
		$color_id  && $where.=" and b.color_id=".$color_id;
		$size_id   && $where.=" and b.size_id=".$size_id;
		$order_id  && $group_by = 'id';
		$p_id 	   && $group_by.=($group_by?',':'').'product_id,size_id,color_id';
		$opert['where']	= $where;
		$opert['group_by'] = $group_by;
		if($order_id){
			$opert['field'] = ' ,if( d.load_state =2, 0, ( c.quantity * c.capability * c.dozen ) ) AS load_quantity, 
							    ( b.quantity * b.capability * b.dozen) as order_qn,
							    ((b.quantity * b.capability * b.dozen) - IFNULL(if( d.load_state =2, 0, ( c.quantity * c.capability * c.dozen ) ),0)
								) AS unload_quantity,group_concat(a.id) as multi_id';
		}else{
			$opert['field'] = ' ,if( d.load_state =2, 0, sum( c.quantity * c.capability * c.dozen ) ) AS load_quantity, 
							    sum( b.quantity * b.capability * b.dozen) as order_qn,
							    (sum(b.quantity * b.capability * b.dozen) - IFNULL(if( d.load_state =2, 0, sum( c.quantity * c.capability * c.dozen ) ),0)
								) AS unload_quantity,group_concat(a.id) as multi_id';
		}
		/// 执行查询操作
		$rs		= $model->detail($opert);
		$rs['show_capbility']	= 0;
		$rs['show_delivery']	= 0;
		$rs['show_color']		= 0;
		$rs['show_size']		= 0;
		if(C('instock.delivery')==2){
			/// 取产品每箱数量、每包数量
			$temp 					= M('Product')->field('weight,(cube_long*cube_wide*cube_high) as per_size')->find($p_id);
			$temp					= _formatList(array($temp));
			$rs['per_size'] 		= $temp['list'][0]['edml_per_size'];
			$rs['weight'] 			= $temp['list'][0]['edml_weight'];
			$rs['show_delivery']	= true;
		}
		if(C('order.storage_format')==C('loadContainer.storage_format')) $rs['show_capbility']	= 1;
		if(C('order.color')==1){
			$rs['show_color']	= 1;
		}else{
			if (C('product_color')==1){
				$rs['show_color']	= 2;	
			}
		}
		if(C('order.size')==1){
			$rs['show_size']	= 1;
		}else{
			if (C('product_size')==1){
				$rs['show_size']	= 2;
			}
		}
		unset($model,$temp);
		echo json_encode($rs);
		exit() ;
	}

	/// 获取装柜/整单装柜信息
	public  function getLoadDetails() {
		$detail_id 	= intval($_GET['detail_id']);
		$order_id	= intval($_GET['order_id']);
		/// 实例化类
		$model = D('LoadContainer');
		///构造查询条件
		$where		= ' detail_state<3 ';
		$detail_id && $where	.=" and b.id=".$detail_id;
		$order_id  && $where	.=" and a.id=".$order_id;
		/// 执行查询操作
		$rs['list']			= $model->getDetail($where);
		if(C('loadContainer.storage_format')==C('order.storage_format')){
			$rs['storage_format']	= 1;
		}
		if(C('instock.delivery')==2){
			$rs['show_delivery']	= 1;	
		}
		unset($model);
		echo json_encode($rs);
		exit() ;
	}


	///获取客户信息
	public function getClientInfo() {
		if ($_GET['id'] > 0) {
			$rs = M('Company')->where('comp_type=1')->find(intval($_GET['id']));
///			if (C('multi_client')) {
///				$rs = $this->_formatArray($rs);
///			}
			$rs = _formatArray($rs); 
			if(C('sale.need_pay')==1){
				$rs['client_money']	= D('SaleOrder')->getClientPaidDetail(intval($_GET['id']),intval($_GET['basic_id'])); 
			}else {
				$rs['client_money'] = '';
			}
			echo json_encode($rs);
			exit();
		}
		exit();
	}

	/// 通过条形码获取产品信息
	public function getProductByBarcode() {
		$info = inputBarcode(trim($_GET['barcode']));
		if ($info !== false) {
			$info = _formatArray($info);
			echo json_encode($info);
		}
		exit();
	}

	/// 根据产品信息获取条形码
	public function getBarcode() {
		$product_id = intval($_POST['p_id']);
		$color_id	= intval($_POST['color_id']);
		$size_id	= intval($_POST['size_id']);
		if ($product_id > 0) { ///
			echo getBarcode($product_id,$color_id,$size_id);
		}
		exit();
	}

	/// 通过条形码找销售单信息
	public function getSaleInfoByBarcode() {
		$info 			= inputBarcode(trim($_GET['barcode']));
		$sale_order_id 	= intval($_GET['sale_order_id']);
		if ($info !== false && $sale_order_id > 0 ) {
			$where = 'product_id='.$info['p_id'].' and sale_order_id='.$sale_order_id;
			$cache_barcode = S('barcode');
			if ($cache_barcode['barcode']['color_no'] >0 && $info['color_id']) {
				$where.=' and color_id='.$info['color_id'];
			}
			if ($cache_barcode['barcode']['size_no'] >0 && $info['size_id']) {
				$where.=' and size_id='.$info['size_id'];
			}
			$sale_info = M('SaleOrderDetail')->field('*,sum(quantity*capability*dozen*pieces) as sum_qn')->group('1')->where($where)->select();
			if ($sale_info['id'] > 0) {
				$sale_info = $this->_formatArray($sale_info);
				echo json_encode($sale_info);
			}
		}
		exit();
	}
	

	/// 设置当前登陆用户的向导是否启用
	public function setGuide(){
		$user_id 			= getUser('user_id');
		$guide				= intval($_POST['guide']);
		$_SESSION['LOGIN_USER']['guide']	= $guide;
		echo M('User')->where('id='.$user_id)->setField('guide',$guide);
	}

	/// 获取IVA
	public function getIva() {
		$id 	= intval($_GET['id']);
		$from	= $_GET['from'];
		if ($id > 0) {
			if (C('invoice.'.$from)=='1') {///客户列表  供应商列表
				$table='company';
				$invoice_from	= 0;
			} else{
				$table='InvoiceCompany';
				$invoice_from	= 1;
			}
			$data = M($table)->find($id);
			$data['invoice_from']	= $invoice_from;
			$data['factory_from']	= C('invoice.factory_from');
			$data['product_factory']= C('product_factory');
			echo json_encode($data);
			exit();
		}
	}

	/// 根据产品ID获取产品的尾箱信息，尾箱指与产品基本信息中配置不一致的规格，如果产品信息未配置不处理
	public function getLastStorage(){
		$product_id = intval($_GET['id']);
		$state 		= intval($_GET['state']);
		if(empty($product_id)) return false;
		$where = 'product_id='.$product_id.' and quantity>0 and mantissa='.$state;
		$info = M('SaleStorage')->where($where)->select();
		if (count($info)>1) {	/// 多条记录时客户自由选择，无需返回值
			echo json_encode(array('state'=>2,'data'=>array()));
		}elseif (count($info)==1) {
			echo json_encode(array('state'=>1,'data'=>$info[0]));
		}else {
			echo '';
		}
		exit();
	}

	/// 根据产品ID取得产品的默认数值
	public function getProductDefaultStorage(){
		$product_id 	= intval($_GET['id']);
		$flow 			= trim($_GET['flow']);
		$state 			= trim($_GET['mantissa']);
		if(empty($product_id)) return false;
		if ($flow=='sale' || $flow=='sale_return') {
			$temp = M('SaleStorage')->where('product_id='.$product_id.' and quantity>0 and mantissa='.$state)->select();
			if (count($temp)==1) {
				$p_info['capability'] = $temp[0]['capability'];
				$p_info['dozen'] = $temp[0]['dozen'];
				$p_info['autocomplete'] = false;
			}else if(empty($temp)){
				$p_info['capability'] = 0;
				$p_info['dozen'] = 0;
				$p_info['autocomplete'] = false;
			}else {
				$p_info['capability'] = 0;
				$p_info['dozen'] = 0;
				$p_info['autocomplete'] = true;
			}
		}else {
			if (C('STORAGE_FORMAT')==3) {
				$capability = 'floor(capability/dozen) as capability';
			}else{
				$capability = 'capability';
			}
			$p_info = M('Product')->field($capability.',dozen')->where('id='.$product_id)->select();
			$p_info['autocomplete'] = false;
		}
		if (count($p_info)>0) {
			echo json_encode($p_info);
		}
		exit;
	}
	/// 获取库存状态
	public function getStorageState() {
		$product_id = intval($_GET['product_id']);
		$capability = intval($_GET['capability']);
		$dozen 		= intval($_GET['dozen']);
		$warehouse_id = intval($_GET['warehouse_id']);
		if (empty($product_id) || empty($capability) || empty($dozen)) {
			return false;
		}else {
			$rs = M('Storage')->field('mantissa')->where('product_id='.$product_id.' and quantity>0 and capability='.$capability.' and dozen='.$dozen.' and warehouse_id='.$warehouse_id)->select();
			echo $rs['mantissa'];
		}
		exit;
	}

	
	/// 获取产品颜色
	public function getProductColorId(){
		$product_id = intval($_POST['id']);
		$list = M('ProductColor')->where('product_id='.$product_id)->select();
		$result = array(0);
		foreach ((array)$list as $value) {
			$result[] = $value['color_id'];
		}
		echo implode(',',$result);
	}
	/// 获取产品尺码
	public function getProductSizeId(){
		$product_id = intval($_POST['id']);
		$list = M('ProductSize')->where('product_id='.$product_id)->select();
		$result = array(0);
		foreach ((array)$list as $value) {
			$result[] = $value['size_id'];
		}
		echo implode(',',$result);
	}
	/// 新装柜时获取订单厂家，币咱
	public function getOrderFacCur(){
		$order_id = intval($_GET['id']);
		$rs = M('Orders')->field('id,factory_id,currency_id')->find($order_id);
		echo json_encode(_formatArray($rs));
	}
	
	
	/// 根据厂家ID获取对应的币种ID
	public function getFacCurrencyId(){
		$fac_id		=	$_GET['id'];
		if ($fac_id>0){
			$currency['id']					= M('Company')->where('id='.$fac_id)->getField('currency_id');
			$array							= S('currency');
			$currency['no']					= $array[$currency['id']]['currency_no'];
			$currency['product_factory']	= 0;
			if(C('product_factory')==1){
				$currency['product_factory']	= 1;
			}
		}
		echo json_encode($currency);
		exit();
	}
	
	/// 根据产品ID获取产品名称和单价
	public function getProductInfoById(){
		$p_id	= $_GET['id'];
		if($p_id>0){ 
			$rs	= M('Product')->field('instock_price,product_name')->find($p_id);
			echo json_encode(_formatArray($rs));
			exit();
		}
		exit();
	}
	
	/// 新装柜时获取订单产品
	public function getOrderProduct(){
		$order_id = intval($_GET['id']);
		$rs = M('Orders')->field('p_id')->find($order_id);
		echo json_encode(_formatArray($rs));
	}
	/// 获取仓库名称
	public function getWareName(){
		$ware_id	= intval($_POST['id']);
		if($ware_id>0){ 
			$ware_arr	= S('warehouse');
			echo $ware_arr[$ware_id]['w_name'];exit();
		}
		exit();
	}
	
	/// 销售单的款项验证
	public function checkVaildSale(){
		$model	= D('SaleOrder');  
		$rs		= $model->validFunds($_POST);
		echo 	json_encode($rs);
		exit();
	}
	/// 发票获取导入信息
	public function getRelationInfo(){
		$id		= intval($_POST['relation_id']);
		$type	= $_POST['import_type'];
		$factory_id = intval($_POST['factory_id']);
		switch ($type){
			case 'instock':
				$model	= D('InvoiceIn');
				$model->setId($id);
				$info	= $model->getInstock($factory_id);
				break;
			case 'saleorder':
				$info	= D('InvoiceOut')->getSaleInfo($id);
				break;
			case 'return':
				$info	= D('InvoiceOut')->getReturnInfo($id);
				break;
			default:
				$info	= array();break;
		}
		echo json_encode($info);
		exit;
	}
	///销售预付款转账获取银行简称,暂时
	function getBankName(){
		$id	= intval($_POST['id']);
		if($id>0){ 
			$bank_name	= S('bank');
			echo $bank_name[$id]['account_name'];
		}
		exit();
	}
	/// 获取发票产品信息
	public function getInvoiceProductInfo(){
		$id		= $_GET['id'];
		$info	= M('InvoiceProduct')->find($id);
		echo json_encode($info);
		exit;
	}
	
	/******************付款开始*************************/
	/// 付款后台验证(客户/厂家/物流)
	public function checkVaildPaid(){
		$model	= D('ObjectFunds');  
		$rs		= $model->validPaidFunds($_POST);
		echo 	json_encode($rs);
		exit();
	}
	/******************付款结束*************************/
	
	/// 实际库存分显示时，展开明细
	public function storageExtend(){
		$where = json_decode('{'.$_POST['data'].'}',true);
		unset($_POST['data']);
		foreach ($where as $field => $value) {
			if($field=='stop_date'){
				$_POST['stop_date'] = $value;
			}else {
				$_POST['query']['a.'.$field] = $value;
			}
		}
		$this->list = D('StorageShow')->getExpand();
		echo '</pre>';
		$this->display();
	}
	
	/// 多个库存显示
	public function multiStorage(){
		$this->pid = intval($_POST['id']);
		$this->list = _formatList(M('Storage')->field('*,quantity*capability*dozen as real_storage')->where('product_id='.$this->pid.' and quantity!=0')->order('product_id,color_id,size_id,capability,dozen,mantissa')->select());
		$this->display();
	}
	
	/// 底部工具条－开始
	public function todayRemind(){
		///addL('remind');
		$url	=	'index.php/'; 
		$today	=	date('Y-m-d'); 
		$rs['today_instock']			=	D('Remind')->todayInstock(); 	
		$rs['today_sale']				=	D('Remind')->todaySale();
		$rs['today_advance']			=	D('Remind')->todayAdvance();
		$rs['today_arrearage']			=	D('Remind')->todayArrearage();
		$rs['today_client']				=	D('Remind')->todayClient();
		
///		$rs['today_factory']			=	D('Remind')->todayFactory();
///		$rs['today_logistics']			=	D('Remind')->todayLogistics();

		$rs['today_instock_url']		=	U('/StatInstock/index/sp_date_real_arrive_date/'.$today);  
		$rs['today_sale_url']			=	U('/StatSale/index/sp_date_order_date/'.$today);    
		$rs['today_advance_url']		=	U('/ClientFunds/index/sp_date_paid_date/'.$today);  
		$rs['today_arrearage_url']		=	U('/ClientStat/index/sp_date_paid_date/'.$today);  
		$rs['today_client_url']			=	U('/ClientStat/index/show/0');

		$show	=	1;
		$this->assign('rs',$rs);
		$this->display();
	}
	/// 提醒
	public function getRemindState(){
		$list = null;
		///订货提醒  
	 	if (C('REMIND_ORDER') && C('REMIND_VALUE_ORDER')>=0 ){  
	 		$list['order']				=	D('Remind')->orderList();   
	 	}
	 	///入库提醒
	 	if (C('REMIND_INSTOCK') && C('REMIND_VALUE_INSTOCK')>=0){ 
	 		$list['instock']			=	D('Remind')->instockList(); 
	 	}
	 	///待配货提醒 
	 	if (C('REMIND_PREDELIVERY') && C('REMIND_VALUE_PREDELIVERY')>=0){  
	 		$list['predelivery']	=	D('Remind')->preDeliveryList(); 
	 	}
	 	///待发货提醒
	 	if (C('REMIND_DELIVERY') && C('REMIND_VALUE_DELIVERY')>=0){ 
	 		$list['delivery']		=	D('Remind')->deliveryList(); 
	 	}
	 	///客户待收款提醒
	 	if (C('REMIND_CLIENT')){ 
	 		$list['client']			=	D('Remind')->clientList(); 
	 	}
	 	///厂家待付款提醒
	 	if (C('REMIND_FACTORY')){ 
	 		$list['factory']		=	D('Remind')->factoryList(); 
	 	}
	 	///物流待付款提醒
	 	if (C('REMIND_LOGISTICS')){ 
	 		$list['logistics']		=	D('Remind')->logisticsList(); 
	 	}
	 	if ($list==null) {
	 		echo 0;
	 		exit;
	 	}
	 	///票据到期提醒
///	 	if (C('REMIND_INVOICE') && C('REMIND_VALUE_INVOICE')>=0){ 
///	 		$list['invoice']		=	D('Remind')->invoiceList(); 
///	 	}
	 	///待审核提醒
///	 	if (C('REMIND_AUDIT')){ 
///	 		$list['audit']				=	D('Remind')->auditList(); 
///	 	}
	 	$i = 0;
	 	foreach ($list as $key=>$value) {
	 		$i += count($value['list']);
	 	}
		echo $i>0?'remind':'remind_active';
		exit;
	}
	/// 提醒
	public function getRemind(){
	 	///订货提醒  
	 	if (C('REMIND_ORDER') && C('REMIND_VALUE_ORDER')>=0 ){  
	 		$list['order']				=	D('Remind')->orderList();   
	 	}
	 	///入库提醒
	 	if (C('REMIND_INSTOCK') && C('REMIND_VALUE_INSTOCK')>=0){ 
	 		$list['instock']			=	D('Remind')->instockList(); 
	 	}
	 	///待配货提醒 
	 	if (C('REMIND_PREDELIVERY') && C('REMIND_VALUE_PREDELIVERY')>=0){  
	 		$list['predelivery']	=	D('Remind')->preDeliveryList(); 
	 	}
	 	///待发货提醒
	 	if (C('REMIND_DELIVERY') && C('REMIND_VALUE_DELIVERY')>=0){  
	 		$list['delivery']		=	D('Remind')->deliveryList(); 
	 	}
	 	///客户待收款提醒
	 	if (C('REMIND_CLIENT')){ 
	 		$list['client']			=	D('Remind')->clientList(); 
	 	}
	 	///厂家待付款提醒
	 	if (C('REMIND_FACTORY')){ 
	 		$list['factory']		=	D('Remind')->factoryList(); 
	 	}
	 	///物流待付款提醒
	 	if (C('REMIND_LOGISTICS')){ 
	 		$list['logistics']		=	D('Remind')->logisticsList(); 
	 	}
	 	///票据到期提醒
///	 	if (C('REMIND_INVOICE') && C('REMIND_VALUE_INVOICE')>=0){ 
///	 		$list['invoice']		=	D('Remind')->invoiceList(); 
///	 	}
///	 	待审核提醒
///	 	if (C('REMIND_AUDIT')){ 
///	 		$list['audit']				=	D('Remind')->auditList(); 
///	 	}    
	 	$list = D('Remind')->resetData($list);
	 	foreach ($list as $key => $value) {
	 		echo '<li><a href="javascript:;" onclick="addTab(\''.$value['link_url'].'\',\''.$value['link_title'].'\');$.closeAllToolbar();" title="'.$value['link_title'].'" url="'.$value['link_url'].'">'.$value['caption'].$value['total'].'</a></li>';
	 	}
	 	///$this->display();
	}
	/// 工具栏 产品
	public function toolBarProduct(){
		$product_no = $_GET['no'];
		$role_type = $this->admin ? 1 : $this->login_user['role_type'];
		$this->assign('role_type',$role_type);	
		$rs = M('Product')->where('product_no=\''.$product_no.'\'')->find();
		if (empty($rs)) {
			$state=2;
			$rs = M('Product')->where('product_no like \'%'.$product_no.'%\'')->select();
			foreach ($rs as &$value) {
				$value['product_no_search'] = preg_replace("/(".$product_no.")/i", "<font color=red><b>\\1</b></font>", $value['product_no'],1);
			}
		}else {
			$state = 1;
			$rs = _formatArray($rs);
			if ($role_type==1 || $role_type==3) {
				/// 获取可销售库存信息
				$storage = D('StorageShow')->getProductSaleStorage($rs['id']);
				$this->assign('storage',_formatList($storage));
				/// 获取实际库存
				$group ='warehouse_id,color_id,size_id,mantissa,capability,dozen';
				$storage = M('Storage')->where('quantity != 0 and product_id='.$rs['id'])->group($group)->order('color_id,size_id,mantissa,capability,dozen,quantity')->select();
				$storage = _formatList($storage);
				$this->assign('real_storage',$storage);	
			}
									
		}
		$temp 	= S('currency');
    	$cur	= array('in'=>$temp[1]['currency_no'],'out'=>$temp[C('CURRENCY')]['currency_no']);
    	$this->assign('cur',$cur);
		$this->assign('state',$state);
		$this->assign('rs',$rs);
		$this->display();
	}
	/**
	 * 底部工具条－结束
	 */
	
	public function setIndex(){
		$id = getUser('id');
		$index = $_POST['url'];
		M('User')->where('id='.$id)->setField('index',$index);
		exit;
	}
	/**
	 * 快捷菜单－－开始
	 */
	public function setShortcutMenu(){
		$user_id = getUser('id');
		$model	 = trim($_POST['model']);
		$action  = trim($_POST['action']);
		$info = array('user_id' => $user_id,'model' => $model,'action' => $action);
		M('ShortcutMenu')->delete(array('where'=>'user_id='.$user_id.' and model=\''.$model.'\' and action=\''.$action.'\''));
		echo M('ShortcutMenu')->add($info);
		exit;
	}
	
	public function loadShortcutMenu(){
		$user_id = getUser('id');
		$list = M('ShortcutMenu')->where('user_id='.$user_id)->order('menu_order asc,id desc')->select();
		$total_i = intval($_GET['mt']);
		$i = 1;
		$info = array();
		foreach ($list as $value) {
			if($i>$total_i){
				$info['hidden'][] = array('model'=>$value['model'],'action'=>$value['action'],'menu_name'=>title($value['action'],$value['model']));
			}else{
				$info['short'][] = array('model'=>$value['model'],'action'=>$value['action'],'menu_name'=>title($value['action'],$value['model']));
			}
			$i++;
		}
		$this->assign('short_menu',$info);
		$this->display();
	}
	
	public function updateShortMenu(){
		$info = array();
		$data = trim($_POST['data'],',');
		$temp = explode(',',$data);
		foreach ($temp as $value){
			$temp2 = explode(':',$value);
			M('ShortcutMenu')->save(array('id'=>$temp2[0],'menu_order'=>($temp2[1]+1)));
		}
		return true;
	}
	public function deleteShortMenu(){
		$id = intval($_POST['id']);
		return M('ShortcutMenu')->delete($id);
	}
	/**
	 * 快捷菜单－－结束
	 */
	
	/// autoshow
	public function autoshow(){
		echo '<pre>';
		print_r($_POST);
		echo '</pre>';
	}
	
	public function validRate(){
		$model	= D('Instock');
		$data	= $model->validRate($_POST);
		echo json_encode($data);
		exit;
	}
	
	/// 导入excel
	public function imporExcel() {
		$_GET['file'] 		= trim($_GET['file']);
		$_GET['key'] 		= trim($_GET['key']);
		$_GET['in_type'] 	= trim($_GET['in_type']);
		echo json_encode(A('Excel')->insert());
///		echo R('Excel','insert');
		exit;
	}
	
	///列表导出csv
	public function outPutExcel(){
		$model		= D('Excel');
		$list		= $model->getOutPut();
	    $csvname 	= $_GET['state'].'.csv';   ///文件命名
	    $data		= @implode(',',$list['fields'])."\n";
	   	foreach ((array)$list['list'] as  $k => $v){
			foreach((array)$list['value'] as $key => $value){
				$info .= $info ?  ",".$v[$value] : $v[$value];
			}
			$info	.= "\n";
	   		$data	.= $info;
			$info	= '';
	   	}
	    header("Content-type: text/csv");
	    header("Content-Disposition: attachment; filename=" . $csvname);
	    header('Cache-Control:   must-revalidate,   post-check=0,   pre-check=0');
	    header('Expires:   0');
	    header('Pragma:   public');
	    $data=  iconv("utf-8",'gbk',$data);
	    echo $data;exit;
	}
	
	
	
	/// 计划任务,每天执行计算利润
	function tasksProfit(){  
		$absStat	= new AbsStatPublicModel();
		///本位币
		$base_currency_id	=	 C('currency');
		///销售单
		$absStat->iniProfitSale($base_currency_id);
		///款项
		$absStat->iniProfitFund($base_currency_id);
		///初始化退货利润
		$absStat->iniProfitReturn();
	}
}
?>